import 'dart:convert';

import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:ponydata_desk/utils/api.dart';
import 'package:ponydata_desk/utils/httpUtil.dart';
import 'package:ponydata_desk/utils/utils.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:ponydata_desk/utils/shared_key.dart';
import 'package:bot_toast/bot_toast.dart';

import '../../model/flow_model.dart';
import '../../model/login.dart';
import '../model/position_model.dart';
class LineChartSection extends StatefulWidget {

  ChannelModel? channelModel;

   LineChartSection({super.key,this.channelModel});

  @override
  State<LineChartSection> createState() => _LineChartSectionState();
}

class _LineChartSectionState extends State<LineChartSection> {
  List<FlowModel> _flowModels = [];

  List<Color> gradientColors = [
    Color(0xff017FF4),
    Color(0xff017FF4),
  ];

  @override
  void initState() {
    super.initState();

  }

  @override
  Widget build(BuildContext context) {
    return Container(child:
      LineChart(
        mainData(),
      )
    // )
    );
  }

  Widget bottomTitleWidgets(double value, TitleMeta meta) {
    const style = TextStyle(
      fontWeight: FontWeight.bold,
      fontSize: 10,
      color: Color(0x55000000)
    );

    int index = value.toInt();
    String textStr = "${index}";

    // textStr ="woca";
    // if(_weeks.length>index){
    //   textStr = _weeks[index];
    // }

    Widget text = Text(textStr, style: style, textAlign: TextAlign.left);
    return SideTitleWidget(
      axisSide: meta.axisSide,
      child: text,
    );
  }

  Widget leftTitleWidgets(double value, TitleMeta meta) {
    const style = TextStyle(
      fontWeight: FontWeight.bold,
      fontSize: 10, color: Color(0x55000000)
    );

    return Text("${value.toInt()}", style: style, textAlign: TextAlign.left);
  }
  List<FlSpot> getSpots(){
    List<FlSpot> items = [];
    int index = 0;
    if(widget.channelModel==null){
      return items;
    }
    widget.channelModel!.tems!.forEach((element) {
      items.add(FlSpot(double.parse("${index}"), element),);
      index++;
    });
    print("数据$items");
    return items;
  }

  double getbottomTitleInterval(){
    double interval = 1.0;

    if( widget.channelModel==null){
      return 1.0;
    }
    int count =  widget.channelModel!.tems!.length;
    if(count<10){
      return 1.0;
    }

    return double.parse((widget.channelModel!.tems!.length/10).toString());
  }

  LineChartData mainData() {
    return LineChartData(

      titlesData: FlTitlesData(
        show: true,
        rightTitles: const AxisTitles(
          sideTitles: SideTitles(showTitles: false),
        ),
        topTitles: const AxisTitles(
          sideTitles: SideTitles(showTitles: false),
        ),
        bottomTitles: AxisTitles(
          sideTitles: SideTitles(
            showTitles: true,
            reservedSize: 30,
            interval:getbottomTitleInterval(),
            getTitlesWidget: bottomTitleWidgets,
          ),
        ),
        leftTitles: AxisTitles(
          sideTitles: SideTitles(
            showTitles: true,
            interval: 30,
            getTitlesWidget: leftTitleWidgets,
            reservedSize: 42,
          ),
        ),
      ),
      borderData: FlBorderData(
        show: true,
        border: Border.all(color: const Color(0x55000000)),
      ),
      minX: 0,
      maxX: widget.channelModel!=null? double.parse(widget.channelModel!.tems!.length.toString()):0,
      minY: 0,
      maxY: widget.channelModel!=null? double.parse(widget.channelModel!.max.toString()):0,
      lineBarsData: [
        LineChartBarData(
          spots: getSpots(),
          isCurved: false,
          gradient: LinearGradient(
            colors: gradientColors,
          ),
          barWidth: 2,
          isStrokeCapRound: true,
          dotData: const FlDotData(
            show: false,
          ),
          belowBarData: BarAreaData(
            show: false,
            gradient: LinearGradient(
              colors: gradientColors
                  .map((color) => color.withOpacity(0.8))
                  .toList(),
            ),
          ),
        ),
      ],
    );
  }

}